package com.zhongyouyun.admin.service.imp;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.admin.model.DeptModel;
import com.zhongyouyun.admin.mapper.DeptMapper;
import com.zhongyouyun.admin.model.MenuModel;
import com.zhongyouyun.admin.service.DeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;

/**
 * @Author: shenyi
 * @Date: 2021-12-17
 * @Description: 服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DeptServiceImpl extends ServiceImpl<DeptMapper, DeptModel> implements DeptService {
        /*
         * @Author: shenyi
         * @Date: 2021-09-08
         * @Description: 分页查询
         * @param page 分页数据  lambdaQueryWrapper where 条件 和排序
         */
        @Override
        public IPage<DeptModel> selectPage(Page<DeptModel> page, QueryWrapper<DeptModel> queryWrapper) {
          return this.baseMapper.selectPage(page,queryWrapper);
        }


        /*
         * @Author: shenyi
         * @Date: 2021-09-08
         * @Description: 查询全部
         * @param   lambdaQueryWrapper where 条件 和排序
         */
        @Override
        public List<DeptModel> selectList(QueryWrapper<DeptModel> queryWrapper) {
          return this.baseMapper.selectList(queryWrapper);
        }

        @Override
        public List<Tree<Long>> findDeptTree(DeptModel deptModel) {
            List<DeptModel> deptModelList=this.baseMapper.findDeptTreeList(deptModel);
            TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
            List<Tree<Long>> list = TreeUtil.build(deptModelList,0L,treeNodeConfig,
                    (treeNode,tree) -> {
                        tree.setId(treeNode.getId());
                        tree.setParentId(treeNode.getPId());
                        tree.putExtra("name", treeNode.getDeptName());
                        tree.putExtra("pid", treeNode.getPId());
                        tree.putExtra("deptName", treeNode.getDeptName());
                        tree.putExtra("orderNum",treeNode.getOrderNum());
                        tree.putExtra("createBy",treeNode.getCreateBy());
                        tree.putExtra("createTime",treeNode.getCreateTime());
                        tree.putExtra("updateBy",treeNode.getUpdateBy());
                        tree.putExtra("updateTime",treeNode.getUpdateTime());
                        tree.putExtra("parentName","顶级机构");
                        tree.putExtra("remark",treeNode.getRemark());
                        deptModelList.forEach(model->{
                            if(model.getId().equals(treeNode.getPId()))
                                tree.put("parentName",model.getDeptName());
                        });
                    });
            return list;
        }

    @Override
    public String getoid(Long deptId) {
        return this.baseMapper.getoid(deptId);
    }
}
